package com.activities;

import java.util.ArrayList;

import org.json.JSONException;

import com.bean.RIarticles;
import com.bean.article;
import com.dbs.DataHelper;
import com.example.lizhiinformation.R;
import com.example.lizhiinformation.R.drawable;
import com.myadapter.FrontPage_information_adapter;
import com.ownload.Httputil;
import com.ownload.Httputil.OnNetWorkRespones;
import com.tools.FrontPage_JOSN;
import com.tools.Path_URL;
import com.tools.RelativeInformationJson;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 普通的详细新闻界面
 * @author Administrator
 *
 */
public class Front_items_activity extends Activity {

	private Button content;
	private Button back;
	private Button comment;
	private TextView RI1;//相关新闻
	private TextView RI2;
	private TextView RI3;
	private ListView listView;
	private View unloading;
	private View viewtop;
	private TextView title;
	private TextView source;
	private TextView time;
	private String extra;
	private View viewfoot;
	private ArrayList<RIarticles> RIJ;
	private FrontPage_information_adapter adapter;
	private article article;
	private ActionBar actionbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_front_items_activity);
		actionbar =getActionBar();
		actionbar.setIcon(R.drawable.newicon);
		actionbar.setBackgroundDrawable(getResources().getDrawable(R.drawable.about_bg));
		extra = getIntent().getExtras().getString("id");
		FindView();
		load(extra);
		footer();
		relativeInformation();
//		String[] query = new DataHelper(Front_items_activity.this).query(1, "头条");
//		System.out.println("----------"+query[1]);
		
	}

	/**
	 * 数据下载处理
	 */
	public void load(String extra) {
		// 下载items的数据
		Httputil.OnWorkDown(Path_URL.Information_url1 + extra
				+ Path_URL.Information_url2, new OnNetWorkRespones() {

			@Override
			public void OK(String respose) {
				// TODO Auto-generated method stub
				try {

					article = new FrontPage_JOSN().josn(respose);

					title.setText(article.getSubject());
					source.setText(article.getOrigin());
					time.setText(article.getOrigined());
					actionbar.setTitle(article.getSubtitle());
					adapter = new FrontPage_information_adapter(
							Front_items_activity.this, article,listView);
					String id = article.getId();
					Httputil.OnWorkDown(Path_URL.RELATIVEINFORMATION + id
							+ Path_URL.RELATIVEINFORMATION1,
							new OnNetWorkRespones() {

								@Override
								public void OK(String respose) {
									RIJ = new RelativeInformationJson()
											.json(respose);
									if(RIJ.size()==3){
									RI1.setText(RIJ.get(0).getSubject());
									RI2.setText(RIJ.get(1).getSubject());
									RI3.setText(RIJ.get(2).getSubject());
									}
								}

								@Override
								public void ERROR(String error) {
									Front_items_activity.this.finish();

								}
							});
					listView.setAdapter(adapter);
					unloading.setVisibility(View.INVISIBLE);
					listView.setVisibility(View.VISIBLE);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			@Override
			public void ERROR(String error) {
				// TODO Auto-generated method stub

			}
		});
		listView.setDivider(null);
		listView.addHeaderView(viewtop);
		listView.addFooterView(viewfoot);
	}

	/**
	 * 组件的初始化
	 */
	public void FindView() {
		back = (Button) findViewById(R.id.back);
		comment = (Button) findViewById(R.id.comment);
		content = (Button) findViewById(R.id.content);
		viewfoot = getLayoutInflater().from(Front_items_activity.this).inflate(
				R.layout.items_bottom, null);
		RI1 = (TextView) viewfoot.findViewById(R.id.RI1);
		RI2 = (TextView) viewfoot.findViewById(R.id.RI2);
		RI3 = (TextView) viewfoot.findViewById(R.id.RI3);
		Intent intent = getIntent();
		extra = intent.getStringExtra("url");
		listView = (ListView) findViewById(R.id.fromtpage_information_listview);
		unloading = findViewById(R.id.unloading);
		listView.setVisibility(View.INVISIBLE);
		viewtop = LayoutInflater.from(Front_items_activity.this).inflate(
				R.layout.information_top, null);
		title = (TextView) viewtop.findViewById(R.id.information_top_title);
		source = (TextView) viewtop.findViewById(R.id.information_source);
		time = (TextView) viewtop.findViewById(R.id.information_time);

	}

	/**
	 * 底部控件
	 */
	public void footer() {
		// 返回键的点击事件
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Front_items_activity.this.finish();
			}
		});
		// 评论键的点击事件
		content.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Front_items_activity.this,
						Comment.class);
				startActivity(intent);
			}
		});
		comment.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("image/*");
				startActivity(intent);

			}
		});
	}
/**
 * 相关新闻处理，替换该界面的listview内容
 */
	public void relativeInformation() {
		
			
		
		RI1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				extra = RIJ.get(0).getId();
				listView.removeFooterView(viewfoot);
				listView.removeHeaderView(viewtop);
				load(extra);

			}
		});
		RI2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				extra = RIJ.get(1).getId();
				listView.removeFooterView(viewfoot);
				listView.removeHeaderView(viewtop);
				load(extra);

			}
		});
		RI3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				extra = RIJ.get(2).getId();
				listView.removeFooterView(viewfoot);
				listView.removeHeaderView(viewtop);
				load(extra);

			}
		});
		

	}

}
